const scoreResults = (answers, questions,results)=>{
  // 初始化每个评分结果的分数为0
  results.forEach(item => {
    // @ts-ignore
    item.score = 0;
  });

  // 遍历用户答案
  answers.forEach((answer, index) => {
    if (answer !== null) { // 跳过未作答的题目
      const question = questions[index]; // 获取当前题目的信息
      // eslint-disable-next-line @typescript-eslint/no-shadow
      const option = question.options.find(option => option.key === answer); // 查找对应选项
      if (option) {
        const result = option.result; // 结果值
        // 为所有包含此结果值的评分结果加1分
        results.forEach(r => {
          if (r.resultProp.includes(result)) {
            // @ts-ignore
            r.score++;
          }
        });
      }
    }
  });

  // 找出分数最高的评分结果
  let highestScore = 0;
  let bestResult = null;

  results.forEach(result => {
    if (result.score > highestScore) {
      highestScore = result.score;
      bestResult = result;
    }
  });

  return bestResult;
}

export default scoreResults;

/*
const answerList = ["A"];
const questions = [
  {
    title: "你通常更喜欢",
    options: [
      {
        result: "I",
        value: "独自工作",
        key: "A",
      },
      {
        result: "E",
        value: "与他人合作",
        key: "B",
      },
    ],
  },
];
const question_results = [
  {
    resultProp: ["I", "S", "T", "J"],
    resultDesc: "忠诚可靠，被公认为务实，注重细节。",
    resultPicture: "icon_url_istj",
    resultName: "ISTJ（物流师）",
  },
];

console.log(scoreResults(answerList, questions, question_results));
*/
